package org.lemon.transport.mqtt.server.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.lemon.transport.context.service.TransportContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@Component
public class MqttTransportContext extends TransportContext {

    @Getter
    protected final ObjectMapper mapper = new ObjectMapper();

    @Getter
    @Setter
    @Autowired(required = false)
    private MqttSslHandlerProvider sslHandlerProvider;

    @Getter
    @Setter
    @Value("${transport.mqtt.netty.max_payload_size}")
    private Integer maxPayloadSize;

    @Getter
    @Setter
    @Value("${transport.executor.pool_size:15}")
    private Integer poolSize;

    private final AtomicInteger connectionsCounter = new AtomicInteger();

    public boolean checkAddress(InetSocketAddress address) {
        return true;
    }

    public void channelRegistered() {
        connectionsCounter.incrementAndGet();
    }

    public void channelUnregistered() {
        connectionsCounter.decrementAndGet();
    }
}
